package main;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferStrategy;
import java.util.LinkedList;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class MainGame extends Canvas {
	
	long lastLoopTime;
	BufferStrategy strategy;
	
	LinkedList<Entity> entities;
	
	ShipEntity ship;
	
	boolean gameRunning;
	
	/** The message to display which waiting for a key press */
	private String message = "";
	/** True if we're holding up game play until a key has been pressed */
	private boolean waitingForKeyPress = true;
	/** True if the left cursor key is currently pressed */
	private boolean leftPressed = false;
	/** True if the right cursor key is currently pressed */
	private boolean rightPressed = false;
	/** True if the up cursor key is currently pressed */
	private boolean upPressed = false;	
	/** True if we are firing */
	private boolean firePressed = false;
	/** True if game logic needs to be applied this loop, normally as a result of a game event */
	private boolean logicRequiredThisLoop = false;	
	
	/** The speed at which the player's ship should move (pixels/sec) */
	public double moveSpeed = 300;	
	
	public double fallSpeed = 50;
	
	public double thrustSpeed = -350;

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		MainGame g = new MainGame();
//		g.gameLoop();
	}
	
	public MainGame() {
		entities = new LinkedList<Entity>();
		initEntities();
		JFrame container = new JFrame("Mayhem");
		container.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
		JPanel panel = (JPanel)container.getContentPane();
		panel.setPreferredSize( new Dimension( 800,600 ) );
		panel.setLayout( null );
		
		setBounds(0,0,800,600);
		panel.add(this);
		
		setIgnoreRepaint( true );
		
		container.pack();
		container.setResizable( false );
		container.setVisible( true );
		
		addKeyListener(new KeyInputHandler());
		
		requestFocus();
		
		createBufferStrategy(2);
		strategy = getBufferStrategy();
		
		gameRunning = true;
		
		gameLoop();
		
	}
	
	public double getGravity() {
		return fallSpeed;
	}

	private void gameLoop() {
		
		lastLoopTime = System.currentTimeMillis();
		
		while ( gameRunning ) {
		
			long delta = System.currentTimeMillis() - lastLoopTime;
			lastLoopTime = System.currentTimeMillis();
			
			Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
			g.setColor( Color.BLACK );
			g.fillRect( 0, 0, 800, 600 );
			
			ship.setHorizontalMovement(0);
			
			if ((leftPressed) && (!rightPressed)) {
				ship.rotate( 0 );
//				ship.setHorizontalMovement(-moveSpeed);
			} else if ((rightPressed) && (!leftPressed)) {
				ship.rotate( 1 );
//				ship.setHorizontalMovement(moveSpeed);
			}
			
			if ( upPressed ) {
				System.out.println( ship.getVx() );
				if ( ship.getAngle() != ship.getAngleOfThrust() ) {
					if ( (ship.getAngleOfThrust() > ship.getAngle() ) && ( ship.getAngleOfThrust() - ship.getAngle() > 15 ) ) { 
						ship.setVx( ship.getVx() - ( ship.getAngleOfThrust() - ship.getAngle() ) );
					} 
					
					else
					
					if ( (ship.getAngleOfThrust() < ship.getAngle() ) && ( ship.getAngle() - ship.getAngleOfThrust() > 15 ) ) { 
						ship.setVx( ship.getVx() - ( ship.getAngle() - ship.getAngleOfThrust() ) );
					}
					
					else 
					{
						ship.setVx( ship.getVx() - 5 );
					}
										
				}
				ship.setAngleOfThrust();
				ship.setVx( ship.getVx() + 0.3 );
//				ship.setVerticalMovement( thrustSpeed );
			} else if ( !upPressed ) {
				if ( ship.getVx() > 0 ) {
					ship.setVx( ship.getVx() - 0.15 );
				}
				if ( ship.getVx() <= 0 ) {
					ship.setVerticalMovement( fallSpeed );
				}
			}
			
			for ( int i = 0; i < entities.size(); i++ ) {
				Entity entity = (Entity) entities.get(i);
				entity.move(delta);
			}
			
			for ( int i = 0; i < entities.size(); i++ ) {
				Entity entity = (Entity) entities.get(i);
				entity.draw(g);
			}
			
			
			g.dispose();
			strategy.show();
			
			try {
				Thread.sleep( 10 );
			} catch( Exception e ) {
				//
			}
			
		}
		
	}
	
	private void initEntities() {
		// create the player ship and place it roughly in the center of the screen
		ship = new ShipEntity(this,"warning.png",48,48);
		ship.setAngle( 0 );
//		ship.setThrust( 0 );
		ship.setVx( 0 );
		ship.setGrav( fallSpeed );
		entities.add(ship);
			
		// create a block of aliens (5 rows, by 12 aliens, spaced evenly)
//		alienCount = 0;
//		for (int row=0;row<5;row++) {
//			for (int x=0;x<12;x++) {
//				Entity alien = new AlienEntity(this,
//	                                             "sprites/alien.gif",
//	                                             100+(x*50),
//	                                             (50)+row*30);
//				entities.add(alien);
//				alienCount++;
//			}
//		}
	}	
	
	private class KeyInputHandler extends KeyAdapter {
		
		@Override
		public void keyPressed(KeyEvent e) {
			if (e.getKeyCode() == KeyEvent.VK_LEFT) {
				leftPressed = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
				rightPressed = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_UP) {
				upPressed = true;
			}						
			if (e.getKeyCode() == KeyEvent.VK_SPACE) {
				firePressed = true;
			}
		} 
			
			
		@Override
		public void keyReleased(KeyEvent e) {			
			if (e.getKeyCode() == KeyEvent.VK_LEFT) {
				leftPressed = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
				rightPressed = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_SPACE) {
				firePressed = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_UP) {
				upPressed = false;
			}
			
		}

		@Override
		public void keyTyped(KeyEvent e) {
			// if we hit escape, then quit the game
			if (e.getKeyChar() == 27) {
				System.exit(0);
			}
		}
	}	
	

}
